package com.content.scaffold.common.config;

import com.content.scaffold.common.web.RequestContext;
import com.content.scaffold.common.web.RequestInfo;
import org.jetbrains.annotations.NotNull;

import java.util.concurrent.Executor;

/**
 * @author xiaojl
 *  线程调度时需要自己设置
 */
public class ContextAwareExecutor implements Executor {

    private final Executor executor;

    public ContextAwareExecutor(Executor executor) {
        this.executor = executor;
    }

    @Override
    public void execute(@NotNull Runnable command) {
        RequestInfo requestInfo = RequestContext.getRequestInfo();
        executor.execute(() -> {
            try {
                RequestContext.setRequestInfo(requestInfo);
                command.run();
            } finally {
                RequestContext.clear();
            }
        });

    }
}
